package com.atguigu.juc.date928;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: shenhao
 * @CreatTime: 2024/9/28 17:55
 * @Version: 1.0
 * 两个线程,线程通信,防止虚假唤醒,使用while判断条件,一个增加一,一个减少一,通过 synchronized wait notifyAll
 */
public class Example {
    public static void main(String[] args) {
        Num num = new Num();
        Thread t1=new Thread(()->{
            for (int result = 0; result < 10; result++) {
                try {
                    num.add();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"生产者");
        t1.start();

        Thread t3=new Thread(()->{
            for (int result = 0; result < 10; result++) {
                try {
                    num.add();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"生产者2");
        t3.start();

        Thread t2=new Thread(()->{
            for (int result = 0; result < 10; result++) {
                try {
                    num.dec();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者");
        t2.start();

        Thread t4=new Thread(()->{
            for (int result = 0; result < 10; result++) {
                try {
                    num.dec();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者2");
        t4.start();
    }
}
/*老版synchronized + wait + notifyAll*/
/*class Num{
    private int num=0;

    public synchronized void add() throws InterruptedException {
        while (num!=0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"生产了"+(++num));
        this.notifyAll();
    }

    public synchronized void dec() throws InterruptedException {
        while (num==0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"消费到了"+(--num));
        this.notifyAll();
    }
}*/

/*
* juc 新版 Lock+Condition.await()+signalAll()*/


class Num{
    private int num=0;
    Lock lock=new ReentrantLock();
    Condition condition=lock.newCondition();
    public  void add() throws InterruptedException {
        lock.lock();
        try {
            while (num!=0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"生产了"+(++num));
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public  void dec() throws InterruptedException {
        lock.lock();
        try {
            while (num==0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"消费到了"+(--num));
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}